using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// obj holds output from file task
    /// </summary>
    [Serializable]
    public class FileOutput:ResultBase
    {
        private string _TgtFilePath;
        /// <summary></summary>
        public string TgtFilePath
        {
            get { return _TgtFilePath; }
            set { _TgtFilePath = value; }
        }
        /// <summary></summary>
        public FileOutput():base(Guid.Empty,Guid.Empty)
        {
            this._TgtFilePath = string.Empty;
        }
        /// <summary></summary>
        public FileOutput(Guid componentID, Guid jobTicketID, string tgtPath) : base(componentID, jobTicketID)
        {
            this._TgtFilePath = tgtPath;
        }

        /// <summary>
        /// serialize
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtPath", this.TgtFilePath);

            return xNode;
        }


        /// <summary>
        /// deserialize
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileOutput output = new FileOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.TgtFilePath = xNode.Attributes.GetNamedItem("TgtPath").Value;

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
